﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;

namespace SFJDNotifyIcon
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private NotifyIcon notifyIcon = null;

        public MainWindow()
        {
            InitializeComponent();
            Ini();
        }

        private void Ini()
        {
            ShowInTaskbar = false;
            //启动监听服务
            new SFJDService.SFJDRun();
            //设置右键窗口的样式
            WindowStyle = WindowStyle.None;
            Width = 500;
            Height = 300;
            //设置系统托盘图标
            notifyIcon = new NotifyIcon();
            notifyIcon.BalloonTipText = "程序开始运行";
            notifyIcon.Text = "司法鉴定办公助手";
            //notifyIcon.Icon = Properties.Resources._ico3;
            notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
            notifyIcon.ShowBalloonTip(2000);
            notifyIcon.Visible = true;
            Hide();
            notifyIcon.MouseClick += NotifyIcon_MouseClick;
            Closed += MainWindow_Closed;
            App.Current.Deactivated += Current_Deactivated;
        }

        /// <summary>
        /// 程序失去焦点后 窗口自动隐藏
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Current_Deactivated(object sender, EventArgs e)
        {
            if (Visibility == Visibility.Visible)
            {
                Hide();
            }
        }

        private void MainWindow_Closed(object sender, EventArgs e)
        {
            notifyIcon.Dispose();
        }

        private void NotifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left://左键点击图标
                    ShowLeft();
                    break;
                case MouseButtons.Right://右键点击图标
                    //ShowRight(e.X, e.Y);
                    RightMenu();
                    break;
            }
        }

        private void ShowLeft()
        {
            bool isStarted = false;
            Process[] pro = Process.GetProcesses();//获取已开启的所有进程
            for (int i = 0; i < pro.Length; i++)//遍历所有查找到的进程
            {
                //判断此进程是否是要查找的进程
                if (pro[i].ProcessName.ToString().ToLower() == "sfjdmanagement")
                {
                    isStarted = true;
                    break;
                }
            }
            if (!isStarted)
            {
                Process p = new Process();
                p.StartInfo.FileName = Environment.CurrentDirectory + "\\SFJDManagement.exe";
                p.Start();
            }
        }

        private void RightMenu()
        {
            //System.Windows.Controls.ContextMenu menu = new System.Windows.Controls.ContextMenu();
            //ToolStripItem item1 = new ToolStripItem()
            //{

            //};
            //item1.Click += Item1_Click;
            //menu.Items.Add(item1);
            //this.ContextMenu = menu;
            //ContextMenu.IsOpen = true;
        }

        private void Item1_Click(object sender, EventArgs e)
        {
            new Win_Debug().Show();
        }

        #region 获取鼠标在屏幕中的位置
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool GetCursorPos(out POINT pt);
        [StructLayout(LayoutKind.Sequential)]
        public struct POINT
        {
            public int X;
            public int Y;
            public POINT(int x, int y)
            {
                X = x;
                Y = y;
            }
        }
        #endregion

        private void ShowRight(int x, int y)
        {
            if(Visibility == Visibility.Visible)
            {
                Hide();
            }
            else
            {
                POINT point = new POINT();
                GetCursorPos(out point);
                Left = point.X - Width;
                Top = SystemInformation.WorkingArea.Height - Height;
                Topmost = true;
                Button but_Debug = GetButton("调试");
                ShowDialog();
                Activate();
            }
        }

        private Button GetButton(string str)
        {
            return new Button()
            {
                Text = str, Width = 50, Height = 50,
            };
        }
    }
}
